package com.liangdemo.web.test.demo;

import lombok.Data;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * List结合Stream流进行操作
 *
 * @author: LiangErLe
 * @date: 2022/8/16
 **/
public class ListDemo {
    @Data
    class User {
        private Integer id;
        private String name;
        private Integer age;
        private String sex;

        public User(Integer id, String name, Integer age, String sex) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
    }

    /**
     * 动态添加list
     *
     * @return
     */
    public List<User> test() {
        User user = new User(1, "张三", 20, "男");
        User user1 = new User(2, "李四", 22, "女");
        User user2 = new User(3, "王五", 26, "男");
        User user3 = new User(4, "薛六", 26, "男");
        User user4 = new User(4, "薛七", 26, "男");
        return Stream.of(user, user2, user1, user3, user4).collect(Collectors.toList());
    }

    /**
     * 获取一个固定长度的list
     * 集合不支持添加，
     * 原理时将数组转换为list
     *
     * @return
     */
    public List<Integer> test1() {
        return Arrays.asList(1, 2, 3, 4, 5);
    }

    /**
     * 取集合中的一列生成一个新的数组
     *
     * @param list
     * @return
     */
    public List<String> test2(List<User> list) {
        return list.stream().map(User::getName).collect(Collectors.toList());
    }

    /**
     * 根据条件少选，过滤
     *
     * @param list
     * @return
     */
    public void test3(List<User> list) {
        System.out.println(list.stream().filter(user -> user.getAge() > 22).collect(Collectors.toList()));
        System.out.println("____________________________");
        list.stream().filter(user -> user.getAge() > 22).forEach(System.out::println);
    }

    /**
     * 分组生成map
     *
     * @param list
     * @return
     */
    public Map<String, List<User>> test4(List<User> list) {
        return list.stream().collect(Collectors.groupingBy(User::getSex, Collectors.toList()));
    }

    /**
     * 求和
     *
     * @param list
     * @return
     */
    public void test5(List<User> list) {
        //基于sum()进行累积求和
        System.out.println(list.stream().mapToInt(User::getAge).sum());
        //基于reduce()进行累积求和
        List<Integer> ages = list.stream().map(User::getAge).collect(Collectors.toList());
        System.out.println(ages.stream().reduce(0, (a, b) -> a + b));
    }

    /**
     * list 转换为 map
     *
     * @param list
     * @return
     */
    public Map<Integer, User> test6(List<User> list) {
        Map<Integer, User> map = new HashMap<>();
        for (User user : list) {
            map.put(user.getId(), user);
        }
        return map;
    }

    public Map<Integer, User> test6_1(List<User> list) {
        Map<Integer, User> map = new HashMap<>();
        list.forEach(e -> {
            map.put(e.getId(), e);
        });
        return map;
    }

    public Map<Integer, User> test6_2(List<User> list) {
        Map<Integer, User> map = new HashMap<>();
        //key,重复问题
        //使用 (key1,key2)->key2 ，当重复时，使用key2覆盖key1
        return list.stream().collect(Collectors.toMap(User::getId, Function.identity(), (key1, key2) -> key2));
    }

    /**
     * 判断
     *
     * @param userList
     * @return
     */
    public void test7(List<User> userList) {
        //是否为空
        System.out.println(userList.isEmpty());
        //是否有年龄小于20的
        System.out.println(userList.stream().anyMatch(user -> user.getId() < 20));
        //是否年龄都大于20，是为true
        System.out.println(userList.stream().allMatch(user -> user.getAge() > 18));
        //结果和allMatch相反
        System.out.println(userList.stream().noneMatch(user -> user.getAge() > 18));
        //年龄大于20的数量
        System.out.println(userList.stream().filter(user -> user.getAge() > 20).count());
    }

    /**
     * 使用distinct关键字去重
     *
     * @param list
     */
    public void test8(List<User> list) {
        System.out.println(list.size());
//        list.stream().map(User::getAge).distinct().collect(Collectors.toList()).forEach(System.out::println);
        System.out.println(list.stream().map(User::getAge).distinct().collect(Collectors.toList()).size());

    }

    /**
     * 截取
     * limit 下标从1开始
     * skip 从0开始
     */
    public void test9() {
        List<String> list = test2(test());
        System.out.println(list);
        //前两位
        System.out.println(list.stream().limit(2).collect(Collectors.toList()));
        //第二个到第五个
        System.out.println(list.stream().skip(2).limit(list.size()).collect(Collectors.toList()));
        //先取前3，在取后2
        System.out.println(list.stream().limit(3).skip(1).collect(Collectors.toList()));
    }

    /**
     * 查找
     */
    public void test10() {
        List<User> list = test();
        System.out.println(list);
        //在流中以短路的方式查找
        //  System.out.println(list.stream().filter(user -> user.getAge() > 20).findAny());
        //名字以薛开头的
        //System.out.println(list.stream().filter(user -> user.getName().startsWith("薛")).collect(Collectors.toList()));
        System.out.println(list.stream().filter(user -> user.getAge() == 26).collect(Collectors.toList()));
        System.out.println("_________________________");
        System.out.println(list);
    }

    /**
     * 遍历
     */
    public void test11(List<User> list) {
        //使用Stream遍历
        list.stream().forEach(System.out::println);
        System.out.println("____________________________");
        for (User user : list) {
            System.out.println(user);
        }
        System.out.println("____________________________");
        //使用forEach
        list.forEach(e -> {
            System.out.println(e);
        });
        System.out.println("____________________________");
    }

    /**
     * 批量修改某一属性的值
     */
    public void test12() {
        List<User> list = test();
        List<User> list1 = new ArrayList<>();


        list1 = list.stream().map((item) -> {
            item.setName("张三");
            return item;
        }).collect(Collectors.toList());
        System.out.println(list1);
    }

    /**
     * 修改数据
     */
    public void test13() {
        List<User> list = test();
        list.stream().filter(dto -> dto.getName().equals("李四")).forEach(dto2 -> dto2.setAge(100000));
        System.out.println(list);
    }

    /**
     * 去重
     */
    public void test14() {
        List<User> list = test();
        List<User> userList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getSex))), ArrayList::new));
        System.out.println(userList);
    }

    public static void main(String[] args) {
        ListDemo listDemo = new ListDemo();
        //  System.out.println(listDemo.test5(listDemo.test()));
//        listDemo.test8(listDemo.test());
        listDemo.test13();
    }


}
